package com.jcj.jcjrpc.core.cluster;

import com.jcj.jcjrpc.core.api.LoadBalancer;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public class RoundRibonLoadBalancer<T> implements LoadBalancer<T> {
    AtomicInteger integer = new AtomicInteger();

    @Override
    public T choose(List<T> providers) {
        if (CollectionUtils.isEmpty(providers)) {
            return null;
        }
        return providers.get(integer.incrementAndGet() % providers.size());
    }
}
